<!DOCTYPE html>
<body>
<p>When the contents of an iframe have focus, the activeElement in the parent document should be the iframe element.</p>
<input id="input-0">
<iframe id="frame-1"></iframe>
<iframe id="frame-2"></iframe>
<pre id="out"></pre>
<script>

var doc0 = document;
var input0 = doc0.getElementById('input-0');

var iframe1 = doc0.getElementById('frame-1');
var doc1 = iframe1.contentDocument;
var input1 = doc1.body.appendChild(doc1.createElement('input'));

var iframe2 = doc0.getElementById('frame-2');
var doc2 = iframe2.contentDocument;
var input2 = doc2.body.appendChild(doc2.createElement('input'));

var iframe3 = doc1.body.appendChild(doc1.createElement('iframe'));
var doc3 = iframe3.contentDocument;
var input3 = doc3.body.appendChild(doc3.createElement('input'));

var iframe4 = doc2.body.appendChild(doc2.createElement('iframe'));
var doc4 = iframe4.contentDocument;
var input4 = doc4.body.appendChild(doc4.createElement('input'));

// Set up IDs for logging.
for (var i = 0; i < 5; i++) {
    window['doc' + i].body.id = 'body-' + i;
    if (i > 0)
        window['iframe' + i].id = 'iframe-' + i;
    window['input' + i].id = 'input-' + i;
}

function describe(obj)
{
    if (obj === document)
        return 'top';
    if (obj.defaultView && obj.defaultView.frameElement)
        return describe(obj.defaultView.frameElement);
    return describe(obj.ownerDocument) + '/' + obj.id;
}

function print(s)
{
    document.getElementById('out').textContent += s + '\n';
}

function assertActiveElement(doc, expected)
{
    if (doc.activeElement === expected)
        print('PASS: ' + describe(doc) + ' document.activeElement is ' + describe(doc.activeElement));
    else
        print('FAIL: ' + describe(doc) + ' document.activeElement is ' + describe(doc.activeElement) + ' but expected ' + describe(expected));
}

if (window.testRunner)
    testRunner.dumpAsText();

print('Focusing ' + describe(input0));
input0.focus();
assertActiveElement(doc0, input0);
assertActiveElement(doc1, doc1.body);
assertActiveElement(doc2, doc2.body);
assertActiveElement(doc3, doc3.body);
assertActiveElement(doc4, doc4.body);

print('\nFocusing ' + describe(input1));
input1.focus();
assertActiveElement(doc0, iframe1);
assertActiveElement(doc1, input1);
assertActiveElement(doc2, doc2.body);
assertActiveElement(doc3, doc3.body);
assertActiveElement(doc4, doc4.body);

print('\nFocusing ' + describe(input2));
input2.focus();
assertActiveElement(doc0, iframe2);
assertActiveElement(doc1, doc1.body);
assertActiveElement(doc2, input2);
assertActiveElement(doc3, doc3.body);
assertActiveElement(doc4, doc4.body);

print('\nFocusing ' + describe(input3));
input3.focus();
assertActiveElement(doc0, iframe1);
assertActiveElement(doc1, iframe3);
assertActiveElement(doc2, doc2.body);
assertActiveElement(doc3, input3);
assertActiveElement(doc4, doc4.body);

print('\nFocusing ' + describe(input4));
input4.focus();
assertActiveElement(doc0, iframe2);
assertActiveElement(doc1, doc1.body);
assertActiveElement(doc2, iframe4);
assertActiveElement(doc3, doc3.body);
assertActiveElement(doc4, input4);
</script>
</body>
